﻿using AutoMapper;
using B.S.BasicData.Domain.SystemUsers;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interfaces;
using B.S.BasicData.Write.Api.Application.Command.SystemUsers.IAMCommand;
using MediatR;

namespace B.S.BasicData.Write.Api.Application.CommandHandler.SystemUsers.IAMCommand
{
    /// <summary>
    /// 新增身份校验管理
    /// </summary>
    public class IAMAddCommandHandler:IRequestHandler<IAMAddCommand,ApiResult<int>>
    {
        private readonly IBaseRepository<IAM_Model> _baseRepositoryIAM;
        private readonly IMapper mapper;
        public IAMAddCommandHandler(IBaseRepository<IAM_Model> iAMbaseRepository, IMapper mapper)
        {
            _baseRepositoryIAM = iAMbaseRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 新增身份校验管理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<int>> Handle(IAMAddCommand request, CancellationToken cancellationToken)
        {
            var res=new ApiResult<int>();
            request.UserState = "匹配成功";
            var entity=mapper.Map<IAM_Model>(request);
            var addIAM = _baseRepositoryIAM.Add(entity);
            if (addIAM > 0) 
            {
                res.Msg = "添加成功";
                res.Code = ApiEnums.Success;
                res.Data = addIAM;
            }
            return Task.FromResult(res);
        }
    }
}
